package com.foreknow.view;

import com.foreknow.bean.Guestbook;
import com.foreknow.service.impl.GuestbookServiceImpl;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class MainWindow extends JFrame {

	private JPanel contentPane;
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow frame = new MainWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainWindow() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setForeground(Color.BLACK);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel label = new JLabel("\u540E\u53F0\u7BA1\u7406\u7CFB\u7EDF");
		label.setForeground(Color.GRAY);
		label.setFont(new Font("微软雅黑", Font.BOLD, 12));
		label.setBounds(0, 0, 109, 22);
		contentPane.add(label);

		JButton btnNewButton = new JButton("\u67E5\u8BE2");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//select
				Select s = new Select();
			}
		});
		btnNewButton.setBounds(131, 21, 93, 23);
		contentPane.add(btnNewButton);

		JButton button = new JButton("\u6DFB\u52A0");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//insert
				Insert insert = new Insert();
			}
		});
		button.setBounds(16, 228, 93, 23);
		contentPane.add(button);

		JButton button_1 = new JButton("\u4FEE\u6539");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Update update = new Update();
				//update
			}
		});
		button_1.setBounds(131, 228, 93, 23);
		contentPane.add(button_1);

		JButton button_2 = new JButton("\u5220\u9664");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Delete delete = new Delete();
				//delete
			}
		});
		button_2.setBounds(250, 228, 93, 23);
		contentPane.add(button_2);

		table = new JTable();
		GuestbookServiceImpl guestbookService = new GuestbookServiceImpl();
		List<Object> list = guestbookService.allquery();
		String[] columnNames =
				{ "id", "name", "phone", "email", "title", "content", "createdtime" };
		Object[][] obj = new Object[list.size()][7];
		for (int i = 0; i < list.size(); i++)
		{
			Guestbook guestbook = (Guestbook) list.get(i);
			for (int j = 0; j < 7; j++)
			{
				switch (j)
				{
					case 0:
						obj[i][j] = guestbook.getId();
						break;
					case 1:
						obj[i][j] = guestbook.getName();
						break;
					case 2:
						obj[i][j] = guestbook.getPhone();
						break;
					case 3:
						obj[i][j] = guestbook.getEmail();
						break;
					case 4:
						obj[i][j] = guestbook.getTitle();
						break;
					case 5:
						obj[i][j] = guestbook.getContent();
						break;
					case 6:
						obj[i][j] = guestbook.getCreatedtime();
						break;
				}
			}
		}
		table = new JTable(obj,columnNames);
		table.setBounds(10, 64, 401, 149);
		contentPane.add(table);

		JButton button_3 = new JButton("\u6A21\u7CCA\u67E5\u8BE2");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//mohu
				Mohuselect mohuselect = new Mohuselect();
			}
		});
		button_3.setBounds(10, 21, 93, 23);
		contentPane.add(button_3);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        this.pack();
	}
}
